var apiHost = wx.getStorageSync('apiHost');
var yunId = wx.getStorageSync('yunId');
var openId = wx.getStorageSync('openId');

var apiUpLoad = wx.getStorageSync('apiUpLoad');        //上传外网图片地址


var star = 0;
var content = "";
var orderNo = "aaaa";
var images = new Array();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: true,    //loading
    isShow: false,
    showModal: false,
    showAlert: false,
    showPic:true
  },
  confirm: function (e) {
    this.setData({
      showAlert: false
    })
  },
  cancel: function (e) {
    this.setData({
      showAlert: false
    })
  },
  uploadImg: function (e) {
    var that = this;
    if (images.length >= 3) {
      that.setData({
        showAlerts: "最多添加3张图片",
        showAlert: true,
        showPic:false
      })
    } else {
      wx.chooseImage({
        count: 2,
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          var tempFilePaths = res.tempFiles[0].path;
          
          that.setData({
            images: images
          })
          that.upload(that, tempFilePaths);
        }
      })
    }
  },
  //商品图片传送到外网
  upload: function (page, path) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
      wx.uploadFile({
        url: apiUpLoad+"image-server/uploadImage/oss",
        filePath: path,
        name: 'file',
        header: { "Content-Type": "multipart/form-data" },
        formData: {
          //和服务器约定的token, 一般也可以放在header中
          'session_token': wx.getStorageSync('session_token')
        },
        success: function (res) {
          console.log(res);
         // var images = that.data.images;
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
          var data = res.data;
          var aa = JSON.parse(data); //page.stringToJson(data);  //字符串转json
          var imgUrl = aa.data.url;
          images.push(imgUrl);
          page.setData({  //上传成功修改显示头像
            imgUrl: imgUrl,
            images: images
          })

        },
        fail: function (e) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
  },
  deleteImg: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    images.splice(index, 1);
    if(images.length<3){
      that.setData({
        showPic:true
      })
    }
    this.setData({
      images: images
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    orderNo = options.orderNo;
    yunId = options.yunId;
    openId = options.openId
    this.setData({
      isShow: false
    })
  },
  //选中评价语时
  checkRadio: function (e) {
    star = e.detail.value;
    if (star != "" && content != "") {
      this.setData({
        isShow: true
      })
    }
  },
  getInput: function (e) {
    content = e.detail.value;
  },
  checkArea: function (e) {
    if (star != "" && content != "") {
      this.setData({
        isShow: true
      })
    }
  },
  formSubmit: function (e) {
    var json = "";//JSON.stringify(images);
    var that = this;
    for (var i = 0; i < images.length; i++) {
      if (json == "") {
        json = images[i];
      } else {
        json = json + "," + images[i];
      }
    }
    wx.request({
      url: apiHost + 'xcx/goods/feedback/add',
      data: {
        yunId: yunId,
        openId: openId,
        orderNo: orderNo,
        content: content,
        star: star,
        images: json
      },
      header: {
        'conttent-type': 'application-json'
      },
      success: function (res) {
        if (res.data.code == "10000") {
          that.setData({
            showModal: true
          })

        }
      }
    })
  },
  fconfirm: function (e) {
    this.setData({
      showModal: false
    })
    // 跳转订单列表
    wx.redirectTo({
      url: '/pages/userOrder/userOrderList/userOrderList?yunId=' + yunId + "&openId=" + openId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})